package asionreachel.cn.smarthomedemo.device;

import android.app.Activity;
import android.view.View;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.ListView;

import com.jeremyfeinstein.slidingmenu.lib.SlidingMenu;

import java.util.LinkedList;

import asionreachel.cn.smarthomedemo.R;
import asionreachel.cn.smarthomedemo.activity.DeviceListActivity;
import asionreachel.cn.smarthomedemo.adapter.CDeviceListAdapter;
import asionreachel.cn.smarthomedemo.base.BaseDevice;
import asionreachel.cn.smarthomedemo.base.Common;
import asionreachel.cn.smarthomedemo.data.CDeviceItem;

/**
 * Created by SuperMa on 2017/6/20.
 */

public class SlidingMenuMgr {
    public static SlidingMenuMgr mSlideringMenuMgr = null;
    public Activity mActivity = null;
    private boolean m_bRun = false;
    public SlidingMenu mSlidingMenu;
    public ListView mPullListView;
    private LinkedList<CDeviceItem> mListItems;
    private String mDeviceTag = "";          //当前显示的设备标示
    private int mSelectPosition = 0;    //选中下标

    public void initMenuLeft(Activity _tp) {
        mActivity = _tp;
        //组件测试
        SlidingMenu menu = new SlidingMenu(_tp);
        mSlidingMenu = menu;
        menu.setMode(SlidingMenu.LEFT);
        // 设置触摸屏幕的模式
        menu.setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);
        menu.setShadowWidthRes(R.dimen.shadow_width);
        //menu.setShadowDrawable(R.color.black);

        // 设置滑动菜单视图的宽度
        menu.setBehindOffsetRes(R.dimen.slidingmenu_offset);
        // 设置渐入渐出效果的值
        menu.setFadeDegree(0.35f);
        menu.attachToActivity(_tp, SlidingMenu.SLIDING_CONTENT);
        //为侧滑菜单设置布局
        menu.setMenu(R.layout.fragent_menu);

        setOnMenuClickListener();
        DeviceListActivity.getInstance().onCreate(mActivity, menu.getMenu());
    }

    public void initMenuLeftRight(Activity _tp, String a_strTag) {
        mActivity = _tp;
        mDeviceTag = a_strTag;
        //组件测试
        SlidingMenu menu = new SlidingMenu(_tp);
        mSlidingMenu = menu;
        menu.setMode(SlidingMenu.LEFT_RIGHT);
        // 设置触摸屏幕的模式
        menu.setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);
        menu.setShadowWidthRes(R.dimen.shadow_width);
//        menu.setShadowDrawable(R.drawable.shadow);
        // 设置滑动菜单视图的宽度
        menu.setBehindOffsetRes(R.dimen.slidingmenu_offset);
        // 设置渐入渐出效果的值
        menu.setFadeDegree(0.35f);
        menu.attachToActivity(_tp, SlidingMenu.SLIDING_CONTENT);
        //为侧滑菜单设置布局
        menu.setMenu(R.layout.fragent_menu);
        setOnMenuClickListener();
        setRightClickListener();
        initRightView();
    }

    public void setDeviceTag(String a_strDeviceTag)
    {
        mDeviceTag = a_strDeviceTag;
    }

    public String getDeviceTag() {
        return mDeviceTag;
    }

    public int getSelectPosition() {
        return mSelectPosition;
    }

    public void setSelectPosition(int a_iSelectPosition) {
        mSelectPosition = a_iSelectPosition;
    }

    //设置菜单点击回调
    public void setOnMenuClickListener() {
        View view = mSlidingMenu.getMenu();
        ImageView llPersonalCenter = (ImageView) view.findViewById(R.id.llPersonalCenter);
        ImageView llAddDevice = (ImageView) view.findViewById(R.id.llAddDevice);
        llPersonalCenter.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Common.openPersonalCenter(mActivity);
                //mSlidingMenu.showContent();
            }
        });

        llAddDevice.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Common.openAddDevice(mActivity);
                //mSlidingMenu.showContent();
            }
        });

        mSlidingMenu.setOnOpenListener(new SlidingMenu.OnOpenListener() {
            @Override
            public void onOpen() {
                m_bRun = true;
                //获取设备信息
                BaseDevice.GetDeviceInfoList(getDeviceTag());
            }
        });
        mSlidingMenu.setOnOpenedListener(new SlidingMenu.OnOpenedListener() {
            @Override
            public void onOpened() {
            }
        });
        mSlidingMenu.setOnCloseListener( new SlidingMenu.OnCloseListener() {
            @Override
            public void onClose() {
                m_bRun = false;
            }
        } );
        mSlidingMenu.setOnClosedListener(new SlidingMenu.OnClosedListener() {
            @Override
            public void onClosed() {
            }
        });
    }

    public void setRightClickListener() {
        View view = mSlidingMenu.getSecondaryMenu();
        ImageButton btDeviceAdd = (ImageButton) view.findViewById(R.id.device_list_add);
        btDeviceAdd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Common.showToast("添加设备功能尚未开通");
                //mSlidingMenu.showContent();
            }
        });
    }

    //初始化设备列表
    public void initRightView() {
        View view = mSlidingMenu.getSecondaryMenu();
        mPullListView = (ListView) view.findViewById(R.id.fragent_device_list);

        mListItems = new LinkedList<CDeviceItem>();

        //ArrayAdapter<String> adapter = new ArrayAdapter<String>(mActivity, android.R.layout.simple_list_item_1, mListItems);

        CDeviceListAdapter adapter = new CDeviceListAdapter(mActivity.getApplicationContext(), R.layout.device_list_item, mListItems);
        //listView.setAdapter(adapter);
        //adapter.notifyDataSetChanged();
        mPullListView.setAdapter(adapter);
    }

    public void addDeviceItem(String a_strDeviceId, String a_strName, boolean a_iOnline, int a_iIconOnline, int a_iIconOffline) {
        if (mActivity == null)
            return;
        CDeviceItem deviceItem = new CDeviceItem(a_strDeviceId, a_strName, a_iOnline, a_iIconOnline, a_iIconOffline);
        int position = getSelectPosition();
        if (position == mListItems.size())
            deviceItem.setIsSelect(true);
        mListItems.add(deviceItem);
    }

    //添加完数据需要更新
    public void notifyDataChanged() {
        if (mActivity == null)
            return;
        CDeviceListAdapter adapter = (CDeviceListAdapter) mPullListView.getAdapter();
        adapter.notifyDataSetChanged();
    }

    //清空设备数据
    public void clearDeviceItems() {
        if (mActivity == null)
            return;
        mListItems.clear();
    }

    public static SlidingMenuMgr getInstance() {
        if (mSlideringMenuMgr == null)
            mSlideringMenuMgr = new SlidingMenuMgr();
        return mSlideringMenuMgr;
    }

    public SlidingMenu getSlidingMenu()
    {
        return mSlidingMenu;
    }

    public boolean IsRun()
    {
        return m_bRun;
    }
}
